<template>
  <view>
    <view class="goods-item">
      <!-- 左侧图片区域 -->
      <view class="goods-item-left">
        <radio v-if="showRadio" :checked="goods.goods_state" color="#c00000"  @click="radioChangeHandler" />
        <image :src="goods.goods_small_logo || defaultPic" mode=""></image>
      </view>
      <!-- 右侧内容区域 -->
      <view class="goods-item-right">
        <view class="goods-name">
          {{goods.goods_name}}
        </view>
        <view class="goods-info-box">
          <text class="goods-price">￥{{goods.goods_price | toFixed}}</text>
          <uni-number-box v-if="showNum" :min="1" :value="goods.goods_count" @change="numberChangeHandler"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    data() {
      return {
        defaultPic:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    filters:{
      toFixed(num){
        return Number(num).toFixed(2)
      }
    },
    props:{
      //商品的信息对象
      goods:{
        type:Object,
        default:{}
      },
      // 定义是否显示radio
      showRadio:{
        type:Boolean,
        default:false
      },
      showNum:{
        type:Boolean,
        default:false
      }
    },
    methods:{
      radioChangeHandler(){
        this.$emit('radio-change',{
          // 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
          // 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
            //商品id
            goods_id : this.goods.goods_id,
            //商品最新的勾选状态
            goods_state:!this.goods.goods_state
        })
      },
      changeCount(e){
        console.log(e);
      },
      numberChangeHandler(e){
        console.log(e);
        this.$emit('num-change',{
          // 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
          // 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
            //商品id
            goods_id : this.goods.goods_id,
            //商品数量+1
            goods_count:+e
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
  display: flex;
  border-bottom: 1px solid #efefef;
  padding: 10px 0;
  .goods-item-left{
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 15px 0 8px;
    image{
      width: 100px;
      height: 100px;
    }
  }
  .goods-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    font-weight: bold;
    .goods-name{
      font-size: 12px;
    }
    .goods-price{
      font-size: 14px;
      color: red;
    }
  }
}
.goods-info-box{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 10px;
}
</style>